var event = require('../../../../utils/event.js');
// pages/biz/wallet/password/password.js
//引入请求接口文件
import Serv from './payForBankServ';
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
import Config from "../../../../utils/config.js";
import util from '../../../../utils/util2'
class timer {
  constructor(totalCount = 0, cbk = null) {
    this.timer = null
    this.totalCount = totalCount
    this.cbk = cbk
    this.init()
  }
  init() {
    this.cbk && this.cbk(this.totalCount)
    this.timer = setInterval(() => {
      this.clock()
    }, 1000)
  }
  clock() {
    if (this.totalCount >= 0) {
      this.totalCount--
    } else {
      clearInterval(this.timer)
    }
    this.cbk && this.cbk(this.totalCount)
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    timeCount: 0,
    password: null,
    seckillProductId: '',
    randomKey: '',
    totalAmount: '',
    model: false,
    bankCardIndex: 0, //选择银行卡index
    bankCardList: [], //银行卡列表
    buttonClicked: false
  },


  showModal: function() {
    console.log(ok)
  },
  // 下一步
  next: function() {
    console.log(888)
    let data = this.data
    let errorMsg = ''
    let {
      seckillProductId,
      randomKey,
      totalAmount,
      password,
      actionType,
      bankCardList,
      bankCardIndex,
      seckillOrderId
    } = this.data
    console.log(password, randomKey)
    if (password) {
      //todo 参数是写死
      let parmas = {
        password,
        totalAmount,
        randomKey,
        bankCardNo: bankCardList[bankCardIndex].linked_acctno, //银行卡卡号
        paymentType: this.flag(actionType),
        seckillProductId,
        seckillOrderId
      }
      this.paymentMoney(parmas)
    } else {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
      })
    }


  },

  pwdFocus: function(e) {
    
    util.navigateStateChange(this, 'inputDisable');
    wx.navigateTo({
      url: `../../pwdCtrl/pwdCtrl?from=/pay/payForBank/payForBank&eventName=payForBankPwdChange`
    })

  },
  //密码控件回调
  pwdCtrlCallback: function() {

  },
  //银行卡列表
  async queryLinkedAcct(parmas) {
    let {
      data
    } = await Serv.queryLinkedAcct(parmas)
    console.log(data)

    let bankCardList = data.linked_acctlist
    //截取银行卡后四位
    for (var i = 0; i < bankCardList.length; i++) {
      bankCardList[i].logourl = `${Config.ossHost}images/logo/${bankCardList[i].linked_bankcode}.png`
      bankCardList[i].four = bankCardList[i].linked_acctno.substr(bankCardList[i].linked_acctno.length - 4)
    }
    this.setData({
      bankCardList
    })
  },


  // 下一步
  async paymentMoney(parmas) {
    console.log(parmas)
    let data = await Serv.paymentMoney(parmas)

    if (data.success) {
      console.log(data)
      let pay = data.data
      let {
        actionType
      } = this.data
      let paymentType = this.flag(actionType)
      let payParmas = `paymentType=${paymentType}&token=${pay.token}&txnSeqno=${pay.txnSeqno}&totalAmount=${pay.totalAmount}&seckillProductId=${pay.seckillProductId}&name=${pay.name}&action=${actionType}&randomKey=${parmas.randomKey}&password=${parmas.password}`
      if (pay.seckillOrderId) {
        payParmas += `&seckillOrderId=${pay.seckillOrderId}`
      }
      wx.navigateTo({
        url: `../paySendMsg/paySendMsg?${payParmas}`,
      })
    } else {
      wx.showToast({
        title: data.msg,
        icon: 'none'
      })
    }
  },
  //判断报名费或定金(后端按金额分类逻辑1，报名费；2，定金)（前端逻辑 1:秒杀=>提交报名费 ; 2:秒杀=>支付定金 ; 3:竞拍=>支付报名费 ; 4:竞拍=>支付定金）
  flag(item) {
    if (item==1||item == 3) {
      return 1
    } else if (item == 2 || item == 4) {
      return 2
    }
  },
  //选择银行卡
  chooseBankCard(e) {
    
    this.setData({
      bankCardIndex: e.currentTarget.dataset.id
    })
    this.dialog.hideDialog();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //页面传id和price和action （1:秒杀=>提交报名费 ; 2:秒杀=>支付定金 ; 3:竞拍=>支付报名费 ; 4:竞拍=>支付定金）   和 name用户名
    let seckillOrderId = options.orderId ? options.orderId : null
    let seckillProductId = options.id ? options.id : null
    this.setData({
      seckillProductId: options.id,
      totalAmount: options.price,
      actionType: options.action,
      name: options.name,
      seckillOrderId
    })
    var that = this;
    //监听密码控件输入
    event.on('payForBankPwdChange', that, function(changedData) {
      console.log('DataChanged trigger...', changedData);
      that.setData({
        password: changedData.pwdMw,
        randomKey: changedData.randomKey,
        pwdlen: changedData.pwdlen,
        pwdMw_new: changedData.pwdMw.substr(0,changedData.pwdlen),
      });
    });
    //钱包的1个接口
    let parmas = {
      user_id: wx.getStorageSync(Config.userIdKey)
    } //todo//user_id=>
    this.queryLinkedAcct(parmas)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //获得dialog组件
    this.dialog = this.selectComponent("#dialog");
  },
  showDialog() {
    this.dialog.showDialog();
    this.setData({
      model: !this.data.model
    })
  },
  hide() {
    this.dialog.hideDialog();
  },
  //取消事件
  _onCancel() {
    console.log('你点击了取消');
    this.dialog.hideDialog();

  },
  //确认事件
  _onOk() {
    console.log('你点击了确定');
    this.dialog.hideDialog();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var that = this;
    //取消监听
    event.remove('payForBankPwdChange', that);

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
